<template>
    <div>
        <div>
            <el-col :span="3">
                <div class="grid-content bg-purple"></div>
                <el-button type="primary" @click="onhandAdd">添加会员</el-button>
            </el-col>
            <el-input style="width: 240px;" placeholder="请输入姓名" v-model="params.userName"></el-input>
            <el-input style="width: 240px;margin-left: 10px;" placeholder="请输入会员号" v-model="params.userNum">
            </el-input>
            <el-input style="width: 240px;margin-left: 10px;" placeholder="请输入电话号码后四位" v-model="params.userTel"></el-input>

            <el-button style="margin-left: 10px;" type="primary" icon="el-icon-search " @click="loads">搜索</el-button>
            <el-button style="margin-left: 10px;" type="warning" icon="el-icon-scissors " @click="reset">重置</el-button>
        </div>
        <br>

        <el-table ref="multipleTable" :data="tableData" stripe style="width: 80%;" tooltip-effect="dark">

            <el-table-column fixed prop="userNum" label="会员号" width="100">
            </el-table-column>
            <el-table-column prop="userHead" label="头像" width="100">
                <template v-slot="scope">
                    <el-avatar v-show="scope.row.userHead" :src="scope.row.userHead"></el-avatar>
                </template>
            </el-table-column>
            <el-table-column prop="userName" label="姓名" width="100">
            </el-table-column>
            <el-table-column prop="userTel" label="电话号码" width="180">
            </el-table-column>
            <el-table-column prop="userAddress" label="住址" width="300">
            </el-table-column>
            <el-table-column prop="userIntegral" label="积分" width="100">
            </el-table-column>
            <el-table-column prop="email" label="邮箱" width="180">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="240" prop="userId">
                <template v-slot="scope">
                    <el-button type="primary" @click="orderItem(scope.row.userId)">查看订单
                    </el-button>
                    <el-button type="primary" @click="creatOrder(scope.row.userNum,scope.row.userId)">创建订单
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div style="margin-top: 10px;">
            <el-pagination background :current-page="params.pageNum" :page-size="params.pageSize"
                           layout="prev, pager, next" :total="total" @current-change="handleCurrentChange">
            </el-pagination>
        </div>
        <!-- 新增编辑弹框 -->
        <el-dialog title="添加会员" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
            <el-form :inline="true" :model="vipform" status-icon :rules="rules" ref="vipform"
                     style="width: 80%;"
                     label-width="200px">
                <el-form-item label="姓名" prop="userName">
                    <el-input v-model="vipform.userName" placeholder="请输入姓名"></el-input>
                </el-form-item>

                <el-form-item label="电话号码" prop="userTel">
                    <el-input v-model="vipform.userTel" placeholder="请输入电话号码"></el-input>
                </el-form-item>
                <el-form-item label="住址" prop="userAddress">
                    <el-input v-model="vipform.userAddress" placeholder="请输入住址"></el-input>
                </el-form-item>

            </el-form>
            <div style="text-align-last: center;width: 60%;">
                <el-button @click="closedialog('vipform')" type="danger">取 消</el-button>
                <el-button @click="resetForm('vipform')" type="warning">重置</el-button>
                <el-button style="margin-right: 30px;" type="primary" icon="el-icon-upload2 "
                           @click="addvip('vipform')" size="medium">添加
                </el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import request from '@/request'

    export default {
        name: 'VipInfo',
        data() {
            const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
            var checkTel = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('请输入电话号码'));
                }
                setTimeout(() => {
                    if (!regMobile.test(value)) {
                        callback(new Error('请输入正确的电话号码'));
                    } else {
                        callback();
                    }
                }, 1000);
            };
            return {
                //后端返回的数据
                tableData: [],
                //后端返回的页数
                total: 0,
                //分页模糊查询的请求体
                params: {
                    pageNum: 1,
                    pageSize: 6,
                },
                //判断dialog是否显示
                dialogVisible: false,
                //dialog的表单
                vipform: {
                    userName: '',
                    userTel: '',
                    userAddress: ''
                },
                //dialog的校验规则
                rules: {
                    userName: [{
                        required: true,
                        message: '姓名不能为空',
                        trigger: 'blur'
                    },],
                    userTel: [{
                        validator: checkTel,
                        trigger: 'blur',
                        required: true,
                    },],

                }
            }
        },
        created() {
            this.load()
        },
        methods: {
            //页面加载
            loads() {
                this.params.pageNum = 1
                this.load()
            },
            //初始化页面
            load() {
                request.get('/receptionadmin/vipinfo/page', {
                    params: this.params
                }).then(res => {
                    if (res.code === "200") {
                        this.total = res.data.total;
                        this.tableData = res.data.list;

                    }else {
                        this.$message.error(res.msg)
                    }
                })

            },
            //重置方法
            reset() {
                this.params = {
                    user_name: '',
                    user_num: '',
                    pageNum: 1,
                    pageSize: 6
                }
                this.load()
            },
            //切换页码
            handleCurrentChange(pageNum) {
                this.params.pageNum = pageNum;
                this.load()
            },
            /**
             * 查看订单详情
             * @param userId 用户id
             */
            orderItem(userId){
                this.$router.push({
                    name: 'OrderItem',
                    query: {param: userId}
                })
            },
            //新增方法
            addvip(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        request.post('/user/add', this.vipform).then(res => {
                            if (res.code === '200') {
                                this.$notify.success('新增成功,会员号为'+res.data+",初始密码为111，请通知用户及时修改")
                                this.resetForm(formName) //清空表格数据
                                this.dialogVisible = false; //关闭弹框
                                this.load(); //刷新列表
                            } else {
                                this.$notify.error(res.msg)
                            }
                        })
                    }
                });
            },
            //关闭dialog
            handleClose(done) {
                this.dialogVisible = false;
            },
            //添加按钮，从此处调取添加方法
            onhandAdd() {
                this.resetForm('vipform');
                this.dialogVisible = true;
            },
            //清空表单
            resetForm(formName) {
                this.$nextTick(() => {
                    this.$refs[formName].resetFields();
                })
            },
            //关闭dialog
            closedialog(formName) {
                this.dialogVisible = false;
                this.resetForm(formName) //清空表格数据
            },
            //创建订单
            creatOrder(userNum,userId){
                this.$router.push({
                    name: 'AddOrder',
                    query: {userNum: userNum,userId:userId}
                })
            }
        }
    }
</script>
